package cn.tqfeiyang.smartadmin.business.organization.service;

import cn.tqfeiyang.smartadmin.business.organization.entity.EmployeeEntity;
import cn.tqfeiyang.smartadmin.business.organization.mapper.EmployeeMapper;
import cn.tqfeiyang.smartadmin.system.response.RoleQueryVO;

import cn.tqfeiyang.smartadmin.commons.mybatis.EntityBroadcast;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import jakarta.annotation.Resource;

import java.util.List;

/**
 * 员工分配服务
 *
 * @author tqfeiyang
 * @since 2024-10-18
 */
@Service
public class EmployeeAssignService {

    /**
     * 实体广播对象
     */
    @Resource
    private EntityBroadcast entityBroadcast;

    /**
     * 员工映射接口
     */
    @Resource
    private EmployeeMapper employeeMapper;

    /**
     * 根据员工id，查询角色响应列表
     *
     * @param employeeId 员工id
     * @return 角色响应列表
     */
    public List<RoleQueryVO> queryRolesByEmployeeId(Long employeeId) {
        Assert.notNull(employeeId, "employeeId不能为空");
        return employeeMapper.queryRolesByEmployeeId(employeeId);
    }

    /**
     * 根据员工id，查询角色id列表
     *
     * @param employeeId 员工id
     * @return 角色id列表
     */
    public List<Long> getRoleIdsByEmployeeId(Long employeeId) {
        Assert.notNull(employeeId, "employeeId不能为空");
        return employeeMapper.getRoleIdsByEmployeeId(employeeId);
    }

    /**
     * 根据员工id，分配角色id列表
     *
     * @param employeeId 员工id
     * @param roleIds 角色id列表
     */
    @Transactional(rollbackFor = Exception.class)
    public void assignRoleIdsByEmployeeId(Long employeeId, List<Long> roleIds) {
        Assert.notNull(employeeId, "employeeId不能为空");
        Assert.notNull(employeeId, "roleIds不能为空");
        employeeMapper.assignRoleIdsByEmployeeId(employeeId, roleIds);
        entityBroadcast.clearCache(EmployeeEntity.class);
    }

    /**
     * 根据员工id，删除角色id列表
     *
     * @param employeeId 员工id
     * @param roleIds 角色id列表
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteRoleIdsByEmployeeId(Long employeeId, List<Long> roleIds) {
        Assert.notNull(employeeId, "employeeId不能为空");
        Assert.notNull(employeeId, "roleIds不能为空");
        employeeMapper.deleteRoleIdsByEmployeeId(employeeId, roleIds);
        entityBroadcast.clearCache(EmployeeEntity.class);
    }

    /**
     * 根据员工id，清空所有分配的角色id
     *
     * @param employeeId 员工id
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteAllRoleIdsByEmployeeId(Long employeeId) {
        Assert.notNull(employeeId, "employeeId不能为空");
        employeeMapper.deleteAllRoleIdsByEmployeeId(employeeId);
        entityBroadcast.clearCache(EmployeeEntity.class);
    }
}
